//
//  FileUtils.swift
//  ddx0513
//
//  Created by ejian  on 15/7/21.
//  Copyright (c) 2015年 jiang yongbin. All rights reserved.
//

import Foundation

class FileUtils {
    //保存图片
    class func saveImage(image: UIImage, withFileName imageName: String, ofType fileExtension: String) {
        var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        
        var filePath = path + "/"
        filePath = filePath + "images/"
        
//        if (token != nil && token != "") {
//            filePath = filePath + DBUtils.mem.token + "/images/"
//        } else {
//            filePath = filePath + "images/"
//        }
        
        let fileName = filePath + imageName + "." + fileExtension
        createDir(filePath)
        if (fileExtension.lowercaseString == "png") {
            UIImagePNGRepresentation(image).writeToFile(fileName, options: NSDataWritingOptions.AtomicWrite, error: nil)
        } else if (fileExtension.lowercaseString == "jpg" || fileExtension.lowercaseString == "jpeg") {
            var error: NSError?
            var a = UIImageJPEGRepresentation(image, 1.0).writeToFile(fileName, options: NSDataWritingOptions.AtomicWrite, error: &error)
        } else {
            
        }
    }
    
    //读取图片
    class func loadImage(imageName: String, ofType fileExtension: String) -> UIImage? {
        var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        
        var filePath = path + "/"
        filePath = filePath + "images/"
        
//        if (token != nil && token != "") {
//            filePath = filePath + DBUtils.mem.token + "/images/"
//        } else {
//            filePath = filePath + "images/"
//        }
        
        let fileName = filePath + imageName + "." + fileExtension
        
        if let a = UIImage(contentsOfFile: fileName) {
            return a
        } else {
            return nil
        }
    }
    
    //判断文件夹是否存在，如否，则创建
    class func createDir(fileDir: String) {
        let fileManager = NSFileManager()
        if (!fileManager.fileExistsAtPath(fileDir)) {
            fileManager.createDirectoryAtPath(fileDir, withIntermediateDirectories: true, attributes: nil, error: nil)
        }
    }
    
    //计算文件或文件夹大小，返回B单位
    class func fileSizeForDir(path: String) -> Int64 {
        var fm = NSFileManager.defaultManager()
        var size: Int64 = 0
        var arr = fm.contentsOfDirectoryAtPath(path, error: nil)
        
        if let array = arr {
            for (var i = 0; i < array.count; i++) {
                var fullPath = path.stringByAppendingPathComponent(array[i] as! String)
                var isDir = ObjCBool(true)
                if (!(fm.fileExistsAtPath(fullPath, isDirectory: &isDir) && isDir)) {
                    let fileAttributeDic = fm.attributesOfItemAtPath(fullPath, error: nil) as! [String : AnyObject]
                    size = size + (fileAttributeDic["NSFileSize"] as! NSNumber).longLongValue
                } else {
                    size = size + fileSizeForDir(fullPath)
                }
            }
        }
        
        return size
    }
    
    //计算缓存文件夹大小，返回B单位
    class func calcFoldSize() -> Int64 {
        var cachePath1 = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        var cachePath = cachePath1 + "/"
        
        let cacheSize = fileSizeForDir(cachePath)
        
        return cacheSize
    }
    
    //删除文件或文件夹
    class func removeFiles(path: String) {
        var fm = NSFileManager.defaultManager()
        var arr = fm.contentsOfDirectoryAtPath(path, error: nil)
        
        if let array = arr {
            for (var i = 0; i < array.count; i++) {
                var fullPath = path.stringByAppendingPathComponent(array[i] as! String)
                fm.removeItemAtPath(fullPath, error: nil)
            }
        }
    }
    
    //删除缓存文件夹内文件
    class func removeFiles() {
        var cachePath1 = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String
        var cachePath = cachePath1 + "/"
        
        removeFiles(cachePath)
    }
}